<template>
    <div class="search-result">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <van-cell v-for="(item, ind) in list" :key="ind" :title="item.title" />
      </van-list>
    </div>
  </template>

<script>
import { getSearch } from '@/api/search'
export default {
  name: 'SearchResult',
  components: {},
  props: {
    q: {
      type: String,
      required: true
    }

  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1,
      perPage: 20

    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async onLoad () {
      const { data } = await getSearch({
        page: this.page, // 页码
        per_page: this.perPage, // 每页大小
        q: this.q // 搜索关键字
      })
      const results = data.data.results
      this.list.push(...results)

      // 加载状态结束
      this.loading = false

      if (results.length) {
        this.page++
      } else {
        this.finished = true
      }
    }
  }
}
</script>

  <style scoped lang="less"></style>
